
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for view_all_news.
	''' </summary>
	Public Partial Class view_all_news
		Inherits BaseWebPage
		Protected pageID As Integer = -1
		Protected usr As User
		Protected hlInterviewsViewMore As System.Web.UI.WebControls.HyperLink
		Protected hlNewsViewMore As System.Web.UI.WebControls.HyperLink
		Protected htPageConstants As New Hashtable()
		Protected col As Integer = DirectCast(ArticleItem.ArticleItemType.ritNews, Integer)
		Protected period As DateTime = DateTime.Now

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here

			usr = LoggedUser

			If Constants.SHOW_MUSIC <> Constants.SHOW_ON Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			' init special page
			If Request.Params(Constants.SPECIAL_PAGE_TYPE_ID_PARAM) = Nothing OrElse Request.Params(Constants.SPECIAL_PAGE_TYPE_ID_PARAM) = "" Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If

			Try
				pageID = Convert.ToInt32(Request.Params(Constants.SPECIAL_PAGE_TYPE_ID_PARAM))

				col = Convert.ToInt32(Request.Params("col"))

				If col <> DirectCast(ArticleItem.ArticleItemType.ritInterview, Integer) AndAlso col <> DirectCast(ArticleItem.ArticleItemType.ritNews, Integer) Then
					Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
				End If
			Catch
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End Try

			If pageID = 0 Then
				Dim music As BO.SpecialPage = BO.SpecialPages.GetSpecialPageByName(Constants.MUSIC_PAGE)
				If music <> Nothing Then
					pageID = music.ID
				End If
			End If

			Dim CurrentPage As SpecialPage = SpecialPages.GetSpecialPageByID(pageID)

			If CurrentPage = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If

			Me.initPageTitles(Constants.SPECIAL, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			' init tabs
			ucMusicTabs.ActiveTab = 2
			ucMusicTabs.PageName = Constants.VIEW_ALL_NEWS_PAGE

			' init page title
			litPageName.Text = CurrentPage.Name

			If col = DirectCast(ArticleItem.ArticleItemType.ritInterview, Integer) Then

				ucMusicTabs.ActiveTab = 2
				trNewsList.Visible = False
			Else
				ucMusicTabs.ActiveTab = 1
				trInterviewsList.Visible = False
			End If

			If usr <> Nothing AndAlso usr.Status = Constants.appAdmin Then
				hlAddNews.NavigateUrl = "../" + Constants.ADD_NEWS + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + pageID + "&col=" + Convert.ToInt32(ArticleItem.ArticleItemType.ritNews)
				hlAddInterviews.NavigateUrl = "../" + Constants.ADD_NEWS + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + pageID + "&col=" + Convert.ToInt32(ArticleItem.ArticleItemType.ritInterview)
			Else
				hlAddNews.Visible = False
				hlAddInterviews.Visible = False
			End If

			' init period and last month posted
			'period = SpecialPage.GetLastMonth(id);
			'DateTime curDate = period;
			'string sPeriod = "";

			If Request.Params("p") <> Nothing AndAlso Request.Params("p") <> "" Then
				Dim ppTemp As String = Request.Params("p").ToString()
				Dim mandyear As String() = ppTemp.Split("_"C)
				Dim yearID As Integer = Convert.ToInt32(mandyear(0))
				Dim monthID As Integer = Convert.ToInt32(mandyear(1))

					'DateTime tempPeriod = new DateTime(yearID, quarterID, 1);
					'if( tempPeriod < period)
					'	period = tempPeriod;
				period = New DateTime(yearID, monthID, 1)
			Else
					'period = Common.DateUtilities.GetStartOfQuarter( period.Year, Common.DateUtilities.GetQuarter((Common.Month)period.Month )) ;
				period = Common.DateUtilities.GetStartOfCurrentQuarter()
			End If

			If Not Me.IsPostBack Then
				FillQuartersDropDown()
				If col = DirectCast(ArticleItem.ArticleItemType.ritInterview, Integer) Then
					BindInterviewsGrid()
				Else
					BindNewsGrid()
				End If
			End If
		End Sub

		Protected Sub BindInterviewsGrid()
			'init datagrid with database data ordered param
			dgMusicUsers.DataSource = CreateInterviewsDataSource()
			dgMusicUsers.DataBind()
		End Sub

		Protected Sub BindNewsGrid()
			'init datagrid with database data ordered param
			dgNewsItems.DataSource = CreateNewsDataSource()
			dgNewsItems.DataBind()
		End Sub

		Protected Function CreateInterviewsDataSource() As ICollection
			Dim articleItems As ArrayList = SpecialPage.GetArticleItems(pageID, Convert.ToInt32(ArticleItem.ArticleItemType.ritInterview), period, -1, -1, -1)

			Dim dt As New DataTable()

			If articleItems.Count <= 0 Then
				lblNoArticleItems.Visible = True
				dgMusicUsers.Visible = False
			Else
				Dim dr As DataRow

				dt.Columns.Add(New DataColumn("UserName", GetType(String)))
				dt.Columns.Add(New DataColumn("MemberURL", GetType(String)))
				dt.Columns.Add(New DataColumn("PhotoURL", GetType(String)))
				dt.Columns.Add(New DataColumn("NewsURL", GetType(String)))
				dt.Columns.Add(New DataColumn("InterviewTitle", GetType(String)))
				dt.Columns.Add(New DataColumn("InterviewSubtitle", GetType(String)))
				dt.Columns.Add(New DataColumn("InterviewContent", GetType(String)))
				dt.Columns.Add(New DataColumn("EditLink", GetType(String)))
				dt.Columns.Add(New DataColumn("EditLinkVisibility", GetType(Boolean)))
				dt.Columns.Add(New DataColumn("NewsID", GetType(Integer)))
				dt.Columns.Add(New DataColumn("OnClickJS", GetType(String)))

				Dim user As User = Nothing
				Dim grp As Group = Nothing
				Dim pic As UserPicture
				Dim grpPic As GroupPicture
				For Each result As ArticleItem In articleItems
					dr = dt.NewRow()

					If result.UserID > 0 Then
						user = BO.Users.GetUserByID(result.UserID)
						dr(0) = HttpUtility.HtmlEncode(user.Name)

						dr(1) = Users.ComposeMemeberLink(user)
						dr(10) = Users.ComposeMemeberLinkOnClickJS(user, trUserCanceledMessage)

						pic = user.DefaultPicture
						If pic <> Nothing Then
							dr(2) = Pictures.GetPictureSrcFullURL(pic, True, "../")
						Else
							dr(2) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
						End If
ElseIf result.GroupID > 0 Then
						grp = BO.Groups.GetGroupByID(result.GroupID)
						dr(0) = HttpUtility.HtmlEncode(grp.Name)
						dr(1) = "../" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + grp.ID
						grpPic = grp.DefaultPicture
						If grpPic <> Nothing Then
							dr(2) = Pictures.GetPictureSrcFullURL(grpPic, True, "../")
						Else
							dr(2) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
						End If
					End If
					dr(3) = "../" + Constants.VIEW_NEWS + "?id=" + result.ID + "&" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + pageID + "&col=" + Convert.ToInt32(result.Type)
					dr(4) = HttpUtility.HtmlEncode(result.Title)
					'Server.HtmlDecode(result.Title);
					dr(5) = HttpUtility.HtmlEncode(result.Subtitle)
					'Server.HtmlDecode(result.Subtitle);
					dr(6) = special_page.getNewsStart(Server.HtmlDecode(result.Content))
					dr(7) = "../" + Constants.ADD_NEWS + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + pageID + "&id=" + result.ID + "&col=" + Convert.ToInt32(result.Type)
					dr(8) = False
					If usr <> Nothing AndAlso usr.Status = Constants.appAdmin Then
						dr(8) = True
					End If
					dr(9) = result.ID
					' init with newsID
					dt.Rows.Add(dr)
				Next
			End If
			Dim dv As New DataView(dt)
			Return dv
		End Function

		Protected Function CreateNewsDataSource() As ICollection
			Dim middleItems As ArrayList = SpecialPage.GetArticleItems(pageID, Convert.ToInt32(ArticleItem.ArticleItemType.ritNews), period, -1, -1, -1)
			'DateTime.MinValue, -1, -1, -1);
			Dim dt As New DataTable()

			Dim middleItemsCount As Integer = middleItems.Count
			dgNewsItems.AllowSorting = True

			If middleItemsCount <= 0 Then
				lblNoNewsItems.Visible = True
				dgNewsItems.Visible = False
			Else
				Dim dr As DataRow
				dt.Columns.Add(New DataColumn("NewsTitle", GetType(String)))
				dt.Columns.Add(New DataColumn("NewsText", GetType(String)))
				dt.Columns.Add(New DataColumn("FullTextLinkURL", GetType(String)))
				dt.Columns.Add(New DataColumn("FullNewsLinkText", GetType(String)))
				dt.Columns.Add(New DataColumn("EditLink", GetType(String)))
				dt.Columns.Add(New DataColumn("EditLinkVisibility", GetType(Boolean)))
				dt.Columns.Add(New DataColumn("NewsID", GetType(Integer)))
				dt.Columns.Add(New DataColumn("NewsSubtitle", GetType(String)))

				' List news
				For Each result As ArticleItem In middleItems
					dr = dt.NewRow()
					dr(0) = HttpUtility.HtmlEncode(result.Title)
					'Server.HtmlDecode(result.Title);
					dr(1) = special_page.getNewsStart(Server.HtmlDecode(result.Content))
					dr(2) = "../" + Constants.VIEW_NEWS + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + pageID + "&id=" + result.ID + "&col=" + Convert.ToInt32(result.Type)
					dr(3) = "More &gt;"
					dr(4) = "../" + Constants.ADD_NEWS + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + pageID + "&id=" + result.ID + "&col=" + Convert.ToInt32(result.Type)
					dr(5) = False
					If usr <> Nothing AndAlso usr.Status = Constants.appAdmin Then
						dr(5) = True
					End If
					dr(6) = result.ID
					' init with newsID
					dr(7) = HttpUtility.HtmlEncode(result.Subtitle)
					'Server.HtmlDecode(result.Subtitle);
					dt.Rows.Add(dr)
				Next
			End If

			Dim dv As New DataView(dt)
			Return dv
		End Function

		Private Sub dgNewsItems_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
			Dim lbPushed As LinkButton = DirectCast(e.CommandSource, LinkButton)
			Dim cmd As String = e.CommandName
			If cmd = "DeleteNews" Then
				Dim newsInterview As ArticleItem = ArticleItem.GetArticleItemByID(Convert.ToInt32(e.CommandArgument))
				If newsInterview <> Nothing Then
					newsInterview.DoDelete()
				End If
			End If
			Response.Redirect(Constants.VIEW_ALL_NEWS_PAGE + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + pageID + "&col=" + col)
		End Sub

		Protected Sub FillQuartersDropDown()
			ddlQuarters.ClearSelection()

			Dim i As Integer = 0
			While i < Constants.MAX_QUARTER
				Dim startPeriod As DateTime = Common.DateUtilities.GetStartOfQuarter(DateTime.Now.AddMonths(-3 * i).Year, Common.DateUtilities.GetQuarter(DirectCast(DateTime.Now.AddMonths(-3 * i).Month, Common.Month)))
				Dim endPeriod As DateTime = Common.DateUtilities.GetEndOfQuarter(DateTime.Now.AddMonths(-3 * i).Year, Common.DateUtilities.GetQuarter(DirectCast(DateTime.Now.AddMonths(-3 * i).Month, Common.Month)))
				ddlQuarters.Items.Add(New ListItem(String.Format("{0} - {1}", startPeriod.ToString("MMM, yyyy"), endPeriod.ToString("MMM, yyyy")), String.Format("{0}_{1}", startPeriod.Year, startPeriod.Month)))
				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
			End While

			ddlQuarters.SelectedIndex = ddlQuarters.Items.IndexOf(ddlQuarters.Items.FindByValue(String.Format("{0}_{1}", period.Year.ToString(), period.Month)))
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.dgMusicUsers.ItemCommand, AddressOf Me.dgNewsItems_ItemCommand
			AddHandler Me.dgNewsItems.ItemCommand, AddressOf Me.dgNewsItems_ItemCommand
		End Sub
#End Region

		Protected Sub ddlQuarters_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
			Response.Redirect(String.Format("{0}?{1}={2}&col={3}&p={4}", Constants.VIEW_ALL_NEWS_PAGE, Constants.SPECIAL_PAGE_TYPE_ID_PARAM, pageID, col, ddlQuarters.SelectedValue))
			'AutoPostBack="false"
		End Sub

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
		Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated_Form1
			End Get
		End Property
		'set { Migrated_Form1 = value; }
	End Class
End Namespace






